<template>
  <div class="login fn">
    <div class="bgpic df aic jcc">
      <div class="content df fdc jcc">
        <h3>嘴馋特汉堡后台登录</h3>
        <el-form ref="formRef" :rules="rules" :model="formData" size="small">
          <!-- 用户名 -->
          <el-form-item prop="account">
            <el-input
              prefix-icon="iconfont icon-yonghu"
              v-model="formData.account"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              :type="flag ? 'text' : 'password'"
              v-model="formData.password"
              @keyup.13="submit"
            >
              <i slot="prefix" class="iconfont icon-account"></i>
              <i
                @click="flag = !flag"
                slot="suffix"
                class="iconfont"
                :class="flag ? 'icon-eye-open' : 'icon-eye-c'"
              ></i>
            </el-input>
          </el-form-item>
          <!-- 登录 -->
          <el-form-item>
            <el-button @click="submit" type="primary" class="btn">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// 引入vuex解构函数
import {mapActions} from 'vuex'
export default {
  data() {
    return {
      formData: {
        account: '',
        password: '',
      },

      //眼睛
      flag: false,

      rules: {
        account: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur'},
        ],
      },
    }
  },
  methods: {
    ...mapActions('user', ['A_login']),
    submit() {
      this.$refs.formRef.validate(async valid => {
        // 判断
        if (valid) {
          //发送请求
          await this.A_login(this.formData)

          // 跳转操作
          this.$router.push('/home')
        } else {
          console.log('验证失败')
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: #2d3a4b;
  background: url('../../assets/img/login.jpg') no-repeat;
  background-size: cover;

  .bgpic {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
  }
  .content {
    color: #fff;
    background: rgba(255, 255, 255, 0.4);
    width: 340px;
    height: 340px;
    border-radius: 20px;
    padding: 40px 20px;
  }

  h3 {
    text-align: center;
    margin-bottom: 40px;
  }

  .el-button {
    margin-top: 20px;
    width: 100%;
    height: 40px;
    border-radius: 6px;
    background: linear-gradient(to right, rgb(255, 162, 0), rgb(255, 155, 68));
    border: none;
    color: #fff;
  }
}
</style>
